package com.usian.service.Impl;

import com.usian.pojo.TbItem;
import com.usian.redis.RedisClient;
import com.usian.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @author chen
 * @date 2021/1/26 - 22:26 - 星期二
 * @project usian_parent
 * @package com.usian.service.Impl
 * @by GGbomd猪猪侠
 */
@Service
@Transactional
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisClient redisClient;

    @Value("${CART_COOKIE_KEY}")
    private String CART_COOKIE_KEY;

    /**
     * 将购物车存放到redis缓存中
     * @param userId
     * @param cart
     * @return
     */
    @Override
    public Boolean insertCart(String userId, Map<String, TbItem> cart) {
        return redisClient.hset(CART_COOKIE_KEY,userId,cart);
    }

    /**
     * 根据用户 ID 查询用户购物车
     * @param userId
     * @return
     */
    @Override
    public Map<String, TbItem> selectCartByUserId(String userId) {
        return (Map<String, TbItem>) redisClient.hget(CART_COOKIE_KEY,userId);
    }
}
